package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    private Logger logger = LoggerFactory.getLogger(PaymentController.class);
    @Resource
    private PaymentService paymentService;
    /**
     * 创建订单
     * @param payment
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment){
        System.out.println(payment);
        int result = paymentService.create(payment);
        logger.info("数据库返回结果"+result);
        if(result > 0){
            return new CommonResult(200,"success","新增成功");
        }else{
            return new CommonResult(444,"error","新增失败");
        }
    }

    /**
     * 获取订单
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        System.out.println(id);
        List list = paymentService.getPaymentById(id);
        logger.info("数据库返回结果"+list);
        if(list.size() > 0 ){
            return new CommonResult(200,"success",list);
        }else{
            return new CommonResult(444,"error","此ID查询无结果");
        }
    }
}
